﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPSTrackingWebsiteManager.ClassManager;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class AdministratorsSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(List<GridDataItem> rows);
        public event AcceptEvent OnAcceptEvent;

        public delegate void CancelEvent();
        public event CancelEvent OnCancelEvent;

        //Propeties
        [Browsable(true), Category("Custom")]
        public bool IsMultipleSelects { get; set; }

        [Browsable(true), Category("Custom")]
        public string CalledBy { get; set; }

        [Browsable(true), Category("Custom")]
        public bool IsClearGridAfterClose { get; set; }

        //Events
        protected void Page_Load(object sender, EventArgs e)
        {
            AdministratorsGrid.AllowMultiRowSelection = IsMultipleSelects;

            if (!IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.AdministratorsColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.AdministratorsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                if (IsClearGridAfterClose)
                    clearAdministratorsGrid();
            }

        }

        protected void btnAdministratorsSearch_Click(object sender, EventArgs e)
        {
            getInfoGrid();
            AdministratorsGrid.DataBind();
        }

        protected void btnAdministratorsOK_Click(object sender, EventArgs e)
        {
            List<GridDataItem> rows = new List<GridDataItem>();
            foreach (Telerik.Web.UI.GridDataItem selectedItem in AdministratorsGrid.SelectedItems)
            {
                rows.Add(selectedItem);
            }
            if(IsClearGridAfterClose)
                clearAdministratorsGrid();
            OnAcceptEvent(rows);
        }

        protected void btnAdministratorsCancel_Click(object sender, EventArgs e)
        {
            if(IsClearGridAfterClose)
                clearAdministratorsGrid();
            OnCancelEvent();
        }

        protected void AdministratorsGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            getInfoGrid();
        }

        //Methods

        protected void getInfoGrid() {
            if (CalledBy == "AdministratorsByDepartments")
            {
                GetAdministratorsGridToAssociate();
            }
        }

        public void GetAdministratorsGridToAssociate()
        {
            AdministratorsGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.AdministratorsProvider.GetByCompanyIDToAssociateDepartmentsFind(getSessionManager().CompanyIDSelectedComboBox, getSessionManager().AdministratorsByDepartments_AdministratorsSeach_departmentID, txtSearchAdminUsername.Text, txtSearchName.Text, txtSearchFirstName.Text, txtSearchLastName.Text);
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        public void clearAdministratorsGrid()
        {
            AdministratorsGrid.DataSource = new string[] { };
            AdministratorsGrid.DataBind();
        }

        
    }
}